package cn.iocoder.yudao.module.ao.dal.mysql.vocationalclassschedule;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.ao.dal.dataobject.vocationalclassschedule.VocationalClassScheduleDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import cn.iocoder.yudao.module.ao.controller.admin.vocationalclassschedule.vo.*;

/**
 * 中高技班级课 Mapper
 *
 * @author 智教云枢
 */
@Mapper
public interface VocationalClassScheduleMapper extends BaseMapperX<VocationalClassScheduleDO> {

    default PageResult<VocationalClassScheduleDO> selectPage(VocationalClassSchedulePageReqVO reqVO) {
        LambdaQueryWrapperX<VocationalClassScheduleDO> query = new LambdaQueryWrapperX<VocationalClassScheduleDO>()
                .eqIfPresent(VocationalClassScheduleDO::getTerm, reqVO.getTerm())
                .likeIfPresent(VocationalClassScheduleDO::getClassName, reqVO.getClassName())
                .eqIfPresent(VocationalClassScheduleDO::getClassSection, reqVO.getClassSection())
                .eqIfPresent(VocationalClassScheduleDO::getMondayCourse, reqVO.getMondayCourse())
                .eqIfPresent(VocationalClassScheduleDO::getTuesdayCourse, reqVO.getTuesdayCourse())
                .eqIfPresent(VocationalClassScheduleDO::getWednesdayCourse, reqVO.getWednesdayCourse())
                .eqIfPresent(VocationalClassScheduleDO::getThursdayCourse, reqVO.getThursdayCourse())
                .eqIfPresent(VocationalClassScheduleDO::getFridayCourse, reqVO.getFridayCourse())
                .eqIfPresent(VocationalClassScheduleDO::getClassTeacher, reqVO.getClassTeacher())
                .eqIfPresent(VocationalClassScheduleDO::getClassroom, reqVO.getClassroom());
        
        // 教师权限过滤: 班主任匹配 OR 课程字段中包含教师名称
        if (reqVO.getTeacherName() != null && !reqVO.getTeacherName().isEmpty()) {
            String teacherName = reqVO.getTeacherName();
            query.and(w -> w
                    .eq(VocationalClassScheduleDO::getClassTeacher, teacherName)
                    .or().like(VocationalClassScheduleDO::getMondayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getTuesdayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getWednesdayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getThursdayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getFridayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getSaturdayCourse, teacherName)
                    .or().like(VocationalClassScheduleDO::getSundayCourse, teacherName)
            );
        }
        
        query.orderByAsc(VocationalClassScheduleDO::getId);
        
        return selectPage(reqVO, query);
    }

    /**
     * 根据班级名称查询该班级所有教室
     * 用于普通老师权限控制,只能查看自己班级的违纪数据
     *
     * @param className 班级名称,如"2501班"
     * @return 教室列表,如["B213", "B215"]
     */
    @Select("SELECT DISTINCT classroom " +
            "FROM jx_senior_vocational_class_schedule " +
            "WHERE class_name = #{className} " +
            "AND deleted = 0 " +
            "AND classroom IS NOT NULL " +
            "AND classroom != ''")
    List<String> selectClassroomsByClassName(@Param("className") String className);

    /**
     * 根据班主任姓名查询该老师管理的所有班级的教室
     * 用于普通老师权限控制 - 通过remark字段(老师姓名)查询
     *
     * @param teacherName 班主任姓名,如"林晓诗"
     * @return 教室列表,如["B213", "B215"]
     */
    @Select("SELECT DISTINCT classroom " +
            "FROM jx_senior_vocational_class_schedule " +
            "WHERE class_teacher = #{teacherName} " +
            "AND deleted = 0 " +
            "AND classroom IS NOT NULL " +
            "AND classroom != ''")
    List<String> selectClassroomsByTeacherName(@Param("teacherName") String teacherName);

}